package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.jxc.abstracts.AbstractJxcUpdateDataService;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.PaasProcessDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.UserHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.AssociatedBusinessAfterSavePojo;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateVO;
import com.xbongbong.paas.service.ProModelService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.pro.domain.entity.ReRollBackEntity;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.vo.BeforeUpdateVO;
import com.xbongbong.pro.formdata.pojo.vo.UpdateVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.ReturnedPurchaseProductEntity;
import com.xbongbong.saas.domain.entity.ext.ReturnedPurchaseEntityExt;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ReturnedPurchaseEnum;
import com.xbongbong.saas.help.SaasSaveHelp;
import com.xbongbong.saas.help.SaasUpdateHelp;
import com.xbongbong.saas.model.ReturnedPurchaseModel;
import com.xbongbong.saas.model.ReturnedPurchaseProductModel;
import com.xbongbong.saas.service.ReturnedPurchaseService;
import com.xbongbong.workflow.domain.entity.WorkflowBackupDataEntity;
import com.xbongbong.workflow.model.WorkflowBackupDataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getDoubleOrDefaultFromFormData;

/**
 * @author 吴峰
 * @date 2021/02/23 16:03
 */
@Service("returnedPurchaseFormDataUpdateServiceImpl")
public class ReturnedPurchaseFormDataUpdateServiceImpl extends AbstractJxcUpdateDataService {

    private static final Logger LOG = LoggerFactory.getLogger(ReturnedPurchaseFormDataUpdateServiceImpl.class);

    @Resource
    private UserHelp userHelp;
    @Resource
    private ProModelService proModelService;
    @Resource
    private SaasSaveHelp saasSaveHelp;
    @Resource
    private SaasUpdateHelp saasUpdateHelp;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private ReturnedPurchaseModel returnedPurchaseModel;
    @Resource
    private ReturnedPurchaseService returnedPurchaseService;
    @Resource
    private WorkflowBackupDataModel workflowBackupDataModel;
    @Resource
    private ReturnedPurchaseProductModel returnedPurchaseProductModel;



    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.RETURNED_PURCHASE.getCode());
    }

    @Override
    public void afterSaveLog(FormDataUpdateDTO formDataUpdateDTO, PaasFormEntityExt paasFormEntityExt, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity, UpdateVO updateVO, BeforeUpdateVO beforeUpdateVO) throws XbbException {
        RedundantTemplateTypeEnum redundantTemplateTypeEnum = RedundantTemplateTypeEnum.RETURNED_PURCHASE;
        boolean isCoverage = Objects.equals(1, formDataUpdateDTO.getIsCover());
        if(isCoverage){
           return;
        }
        // 记录日志

        String memoName = paasFormEntityExt.getName();
        String userName = updateVO.getUserVO().getName();
        String opObjectName = paasFormDataEntity.getSerialNo();

        String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_WORK), userName, memoName, opObjectName);

        saveLogger(formDataUpdateDTO, paasFormDataEntity, redundantTemplateTypeEnum, memo, opObjectName, OperateModuleTypeEnum.RETURN_PURCHASE, updateVO, beforeUpdateVO,
                OperateTypeEnum.EDIT, IndexTypeEnum.IDX_SAAS_RETURNED_PURCHASE_SUB);

    }

    @Override
    public UpdateVO update(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, PaasFormEntityExt paasFormEntityExt, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        try {
            UserVO userVO = userHelp.getUserName(formDataUpdateDTO.getCorpid(), formDataUpdateDTO.getUserId(), formDataUpdateDTO.getLoginUserName(), beforeUpdateVO.getPaasFormDataEntity().getFlowStatus(), beforeUpdateVO.getPaasFormDataEntity().getCreatorId());
            Integer noApprovalRequired = beforeUpdateVO.getNoApprovalRequired();
            Integer updateLine = 0;
            String corpid = formDataUpdateDTO.getCorpid();
            AssociatedBusinessAfterSavePojo associatedBusinessAfterSavePojo = new AssociatedBusinessAfterSavePojo();
            if ( Objects.equals(noApprovalRequired, 1) ) {
                ReturnedPurchaseEntityExt returnedPurchaseEntityExt = new ReturnedPurchaseEntityExt();
                BeanUtil.copyProperties(paasFormDataEntity, returnedPurchaseEntityExt);
                // 关联产品特殊处理
                JSONObject returnedPurchaseDataList = returnedPurchaseEntityExt.getData();
                JSONArray productArray = saasSaveHelp.formatSelectProduct4Save2(returnedPurchaseDataList, ReturnedPurchaseEnum.PRODUCTS.getAttr());
                //关联付款单发票字段处理
                JSONObject data = paasFormDataEntity.getData();
                JSONArray purchaseInvoiceArray = data.getJSONArray(ReturnedPurchaseEnum.INVOICES.getAttr());
                JSONArray paySheetArray = data.getJSONArray(ReturnedPurchaseEnum.PAY_SHEETS.getAttr());
                saasSaveHelp.formatLinkBusiness4SaveBySerialNo(data, ReturnedPurchaseEnum.INVOICES.getAttr(), ReturnedPurchaseEnum.INVOICES_LINKED_TEXT.getAttr(), ReturnedPurchaseEnum.INVOICES.getFieldType());
                saasSaveHelp.formatLinkBusiness4SaveBySerialNo(data, ReturnedPurchaseEnum.PAY_SHEETS.getAttr(), ReturnedPurchaseEnum.PAY_SHEETS_LINKED_TEXT.getAttr(), ReturnedPurchaseEnum.PAY_SHEETS.getFieldType());
                //合计退款金额在afterSave中处理，根据回款单是否创建成功来处理
                Double returnedPurchaseAmount = getDoubleOrDefaultFromFormData(data, ReturnedPurchaseEnum.RETURNED_PURCHASE_AMOUNT.getAttr(), 0D);

                FormDataUtil.removeSystemData(returnedPurchaseDataList);
                Object richTextData = removeRichText(returnedPurchaseDataList);

                returnedPurchaseEntityExt.setData(returnedPurchaseDataList);
                PaasFormEntity paySheetFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.PAY_SHEET.getCode(), corpid);
                boolean isPaySheetIsProcess = false;
                if (Objects.nonNull(paySheetFormEntity)) {
                    isPaySheetIsProcess = Objects.equals(paySheetFormEntity.getIsProcessForm(), BasicConstant.ONE);
                }
                PaasFormEntity purchaseInvoiceFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.PURCHASE_INVOICE.getCode(), corpid);
                boolean isPurchaseInvoiceIsProcess = false;
                if (Objects.nonNull(purchaseInvoiceFormEntity)) {
                    isPurchaseInvoiceIsProcess = Objects.equals(purchaseInvoiceFormEntity.getIsProcessForm(), BasicConstant.ONE);
                }
                updateLine = returnedPurchaseModel.update(returnedPurchaseEntityExt);
                associatedBusinessAfterSavePojo = new AssociatedBusinessAfterSavePojo(purchaseInvoiceArray, paySheetArray, null, returnedPurchaseAmount,
                        new ReRollBackEntity(), isPaySheetIsProcess, isPurchaseInvoiceIsProcess);
                returnedPurchaseEntityExt.getData().put(ReturnedPurchaseEnum.PRODUCTS.getAttr(), productArray);
                // 设置属性用于返回
                setPartialAttributes(returnedPurchaseEntityExt.getSubFormData(), returnedPurchaseEntityExt.getData(),
                        returnedPurchaseEntityExt.getUpdateTime(), returnedPurchaseEntityExt.getAddTime(), returnedPurchaseEntityExt.getSerialNo(),
                        returnedPurchaseEntityExt.getDepartmentId(), returnedPurchaseEntityExt.getOwnerId(),
                        paasFormDataEntity, beforeUpdateVO);
                //文件柜数据保存
                saveFile(formDataUpdateDTO, paasFormDataEntity, userVO);
                //富文本数据更新
                updateFormRichText(formDataUpdateDTO, richTextData);
            }
            return new UpdateVO(noApprovalRequired, paasFormDataEntity, updateLine, formDataUpdateDTO.getDataId(), userVO, associatedBusinessAfterSavePojo);
        } catch (XbbException e) {
            afterSaveRollBack(formDataUpdateDTO, null, beforeUpdateVO, new ReRollBackEntity());
            throw e;
        } catch (Exception e) {
            afterSaveRollBack(formDataUpdateDTO, null, beforeUpdateVO, new ReRollBackEntity());
            LOG.error("customerFormDataUpdateServiceImpl.update 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }


    @Override
    public FormDataUpdateVO afterSave(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, UpdateVO updateVO, PaasFormEntityExt paasFormEntityExt, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        String corpid = formDataUpdateDTO.getCorpid();
        Long dataId = formDataUpdateDTO.getDataId();
        Integer businessType = formDataUpdateDTO.getBusinessType();
        AssociatedBusinessAfterSavePojo refundAfterSavePojo = updateVO.getAssociatedBusinessAfterSavePojo();
        ReRollBackEntity reRollBackEntity = new ReRollBackEntity();
        if (Objects.nonNull(refundAfterSavePojo)) {
            reRollBackEntity = refundAfterSavePojo.getReRollBackEntity() == null ? new ReRollBackEntity(): refundAfterSavePojo.getReRollBackEntity();
        }
        try {
            FormDataUpdateVO formDataUpdateVO = new FormDataUpdateVO();
            Integer noApprovalRequired = updateVO.getNoApprovalRequired();
            if (Objects.equals(1, noApprovalRequired)) {
                SaasFormSaveDTO saasFormSaveDTO = new SaasFormSaveDTO();

                BeanUtil.copyProperties(formDataUpdateDTO, saasFormSaveDTO);
                packageSaasFormUpdateSaveDTO(saasFormSaveDTO, paasFormDataEntity, oldPaasFormDataEntity);
                ReturnedPurchaseEntityExt returnedPurchaseEntityExt = new ReturnedPurchaseEntityExt();
                returnedPurchaseEntityExt.setId(paasFormDataEntity.getId());
                returnedPurchaseEntityExt.setData(paasFormDataEntity.getData());
                returnedPurchaseEntityExt.setCorpid(paasFormDataEntity.getCorpid());
                returnedPurchaseEntityExt.setSubFormData(paasFormDataEntity.getSubFormData());
                returnedPurchaseEntityExt.setSerialNo(paasFormDataEntity.getSerialNo());
                // 保存后的操作
                returnedPurchaseService.afterSave(saasFormSaveDTO, returnedPurchaseEntityExt, refundAfterSavePojo);
                // 日志
                afterSaveLog(formDataUpdateDTO, paasFormEntityExt, paasFormDataEntity, oldPaasFormDataEntity, updateVO, beforeUpdateVO);
                // api发起
                apiHook(formDataUpdateDTO, updateVO, beforeUpdateVO);

                // 2021/3/3 业务规则
                activeRule(formDataUpdateDTO, beforeUpdateVO, updateVO);
                formDataUpdateVO.setMsg(I18nMessageUtil.getMessage(MessageConstant.FORM_ADD_RETURN_MSG));
                formDataUpdateVO.setCode(PaasConstant.FORM_ADD_RETURN_CODE);
            } else if (Objects.equals(noApprovalRequired, 2)) {
                // 自动拒绝
                formDataUpdateVO.setMsg(I18nMessageUtil.getMessage(MessageConstant.FORM_ADD_PROCESS_RETURN_MSG));
                formDataUpdateVO.setCode(PaasConstant.FORM_ADD_PROCESS_RETURN_CODE);
            } else {
                formDataUpdateVO.setMsg(I18nMessageUtil.getMessage(MessageConstant.FORM_ADD_PROCESS_RETURN_MSG));
                formDataUpdateVO.setCode(PaasConstant.FORM_ADD_PROCESS_RETURN_CODE);
                PaasProcessDataEntity paasProcessDataEntity = new PaasProcessDataEntity();
                paasProcessDataEntity.setCorpid(formDataUpdateDTO.getCorpid());
                paasProcessDataEntity.setFormDataId(dataId);
                paasProcessDataEntity.setBusinessType(businessType);
                paasProcessDataEntity.setSaasMark(formDataUpdateDTO.getSaasMark());
                proModelService.updateFormDataStatus(paasProcessDataEntity, FlowStatusEnum.IN_APPROVAL.getType());
            }
            formDataUpdateVO.setFormDataId(dataId);
            return formDataUpdateVO;
        } catch (XbbException e) {
            afterSaveRollBack(formDataUpdateDTO, updateVO, beforeUpdateVO, reRollBackEntity);
            throw e;
        } catch (Exception e) {
            afterSaveRollBack(formDataUpdateDTO, updateVO, beforeUpdateVO, reRollBackEntity);
            LOG.error("customerFormDataUpdateServiceImpl.afterSave 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public void apiHook(FormDataUpdateDTO formDataUpdateDTO, UpdateVO updateVO, BeforeUpdateVO beforeUpdateVO) throws XbbException {
        // 第三方服务
        lowCodeExecuteService(formDataUpdateDTO, updateVO, beforeUpdateVO);
    }

    @Override
    public UpdateVO updateForWorkflow(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, PaasFormEntityExt paasFormEntityExt, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        try {
            UserVO userVO = userHelp.getUserName(formDataUpdateDTO.getCorpid(), formDataUpdateDTO.getUserId(), formDataUpdateDTO.getLoginUserName(), beforeUpdateVO.getPaasFormDataEntity().getFlowStatus(), beforeUpdateVO.getPaasFormDataEntity().getCreatorId());
            Integer noApprovalRequired = beforeUpdateVO.getNoApprovalRequired();
            Integer updateLine = 0;
            String corpid = formDataUpdateDTO.getCorpid();
            AssociatedBusinessAfterSavePojo associatedBusinessAfterSavePojo = new AssociatedBusinessAfterSavePojo();
            ReturnedPurchaseEntityExt returnedPurchaseEntityExt = new ReturnedPurchaseEntityExt();
            BeanUtil.copyProperties(paasFormDataEntity, returnedPurchaseEntityExt);
            // 关联产品特殊处理
            JSONObject returnedPurchaseDataList = returnedPurchaseEntityExt.getData();
            JSONArray productArray = saasSaveHelp.formatSelectProduct4Save2(returnedPurchaseDataList, ReturnedPurchaseEnum.PRODUCTS.getAttr());
            //关联付款单发票字段处理
            JSONObject data = paasFormDataEntity.getData();
            JSONArray purchaseInvoiceArray = data.getJSONArray(ReturnedPurchaseEnum.INVOICES.getAttr());
            JSONArray paySheetArray = data.getJSONArray(ReturnedPurchaseEnum.PAY_SHEETS.getAttr());
            saasSaveHelp.formatLinkBusiness4SaveBySerialNo(data, ReturnedPurchaseEnum.INVOICES.getAttr(), ReturnedPurchaseEnum.INVOICES_LINKED_TEXT.getAttr(), ReturnedPurchaseEnum.INVOICES.getFieldType());
            saasSaveHelp.formatLinkBusiness4SaveBySerialNo(data, ReturnedPurchaseEnum.PAY_SHEETS.getAttr(), ReturnedPurchaseEnum.PAY_SHEETS_LINKED_TEXT.getAttr(), ReturnedPurchaseEnum.PAY_SHEETS.getFieldType());
            //合计退款金额在afterSave中处理，根据回款单是否创建成功来处理
            Double returnedPurchaseAmount = getDoubleOrDefaultFromFormData(data, ReturnedPurchaseEnum.RETURNED_PURCHASE_AMOUNT.getAttr(), 0D);

            FormDataUtil.removeSystemData(returnedPurchaseDataList);
            Object richTextData = removeRichText(returnedPurchaseDataList);

            returnedPurchaseEntityExt.setData(returnedPurchaseDataList);
            PaasFormEntity paySheetFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.PAY_SHEET.getCode(), corpid);
            boolean isPaySheetIsProcess = false;
            if (Objects.nonNull(paySheetFormEntity)) {
                isPaySheetIsProcess = Objects.equals(paySheetFormEntity.getIsProcessForm(), BasicConstant.ONE);
            }
            PaasFormEntity purchaseInvoiceFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.PURCHASE_INVOICE.getCode(), corpid);
            boolean isPurchaseInvoiceIsProcess = false;
            if (Objects.nonNull(purchaseInvoiceFormEntity)) {
                isPurchaseInvoiceIsProcess = Objects.equals(purchaseInvoiceFormEntity.getIsProcessForm(), BasicConstant.ONE);
            }
            updateLine = returnedPurchaseModel.update(returnedPurchaseEntityExt);
            associatedBusinessAfterSavePojo = new AssociatedBusinessAfterSavePojo(purchaseInvoiceArray, paySheetArray, null, returnedPurchaseAmount,
                    new ReRollBackEntity(), isPaySheetIsProcess, isPurchaseInvoiceIsProcess);
            returnedPurchaseEntityExt.getData().put(ReturnedPurchaseEnum.PRODUCTS.getAttr(), productArray);
            // 设置属性用于返回
            setPartialAttributes(returnedPurchaseEntityExt.getSubFormData(), returnedPurchaseEntityExt.getData(), returnedPurchaseEntityExt.getUpdateTime(), returnedPurchaseEntityExt.getAddTime(), returnedPurchaseEntityExt.getSerialNo(), returnedPurchaseEntityExt.getDepartmentId(), returnedPurchaseEntityExt.getOwnerId(), paasFormDataEntity, beforeUpdateVO);
            //文件柜数据保存
//            saveFile(formDataUpdateDTO, paasFormDataEntity, userVO);
            //富文本数据保存
            Integer updateRichTextLine = updateFormRichText(formDataUpdateDTO, richTextData);
            //表单数据和富文本数据其中有一个编辑就要进工作流
            updateLine = Objects.equals(updateLine, 1) || Objects.equals(updateRichTextLine, 1) ? 1 : 0;

            paasFormDataEntity.getData().put(ReturnedPurchaseEnum.PRODUCTS.getAttr(), productArray);
            paasFormDataEntity.getData().put(ReturnedPurchaseEnum.INVOICES.getAttr(), purchaseInvoiceArray);
            paasFormDataEntity.getData().put(ReturnedPurchaseEnum.PAY_SHEETS.getAttr(), paySheetArray);
            return new UpdateVO(noApprovalRequired, paasFormDataEntity, updateLine, formDataUpdateDTO.getDataId(), userVO, associatedBusinessAfterSavePojo);
        } catch (XbbException e) {
            afterSaveRollBack(formDataUpdateDTO, null, beforeUpdateVO, new ReRollBackEntity());
            throw e;
        } catch (Exception e) {
            afterSaveRollBack(formDataUpdateDTO, null, beforeUpdateVO, new ReRollBackEntity());
            LOG.error("customerFormDataUpdateServiceImpl.update 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public FormDataUpdateVO afterSaveForWorkflow(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, UpdateVO updateVO, PaasFormEntityExt paasFormEntityExt, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        String corpid = formDataUpdateDTO.getCorpid();
        Long dataId = formDataUpdateDTO.getDataId();
        Integer businessType = formDataUpdateDTO.getBusinessType();
        AssociatedBusinessAfterSavePojo refundAfterSavePojo = updateVO.getAssociatedBusinessAfterSavePojo();
        ReRollBackEntity reRollBackEntity = new ReRollBackEntity();
        if (Objects.nonNull(refundAfterSavePojo)) {
            reRollBackEntity = refundAfterSavePojo.getReRollBackEntity() == null ? new ReRollBackEntity(): refundAfterSavePojo.getReRollBackEntity();
        }
        try {
            FormDataUpdateVO formDataUpdateVO = new FormDataUpdateVO();
            Integer noApprovalRequired = updateVO.getNoApprovalRequired();
            SaasFormSaveDTO saasFormSaveDTO = new SaasFormSaveDTO();

            BeanUtil.copyProperties(formDataUpdateDTO, saasFormSaveDTO);
            packageSaasFormUpdateSaveDTO(saasFormSaveDTO, paasFormDataEntity, oldPaasFormDataEntity);
            ReturnedPurchaseEntityExt returnedPurchaseEntityExt = new ReturnedPurchaseEntityExt();
            returnedPurchaseEntityExt.setId(paasFormDataEntity.getId());
            returnedPurchaseEntityExt.setData(paasFormDataEntity.getData());
            returnedPurchaseEntityExt.setCorpid(paasFormDataEntity.getCorpid());
            returnedPurchaseEntityExt.setSubFormData(paasFormDataEntity.getSubFormData());
            returnedPurchaseEntityExt.setSerialNo(paasFormDataEntity.getSerialNo());
            // 保存后的操作
            returnedPurchaseService.afterSaveForUpdateWorkflow(saasFormSaveDTO, returnedPurchaseEntityExt, refundAfterSavePojo);
            // 日志
//            afterSaveLog(formDataUpdateDTO, paasFormDataEntity, updateVO, beforeUpdateVO);
            // api发起
//            apiHook(formDataUpdateDTO, updateVO, beforeUpdateVO);
            // 2021/3/3 业务规则
//            activeRule(formDataUpdateDTO, beforeUpdateVO, updateVO);

            formDataUpdateVO.setMsg(I18nMessageUtil.getMessage(MessageConstant.FORM_ADD_RETURN_MSG));
            formDataUpdateVO.setCode(PaasConstant.FORM_ADD_RETURN_CODE);
//            PaasProcessDataEntity paasProcessDataEntity = new PaasProcessDataEntity();
//            paasProcessDataEntity.setCorpid(formDataUpdateDTO.getCorpid());
//            paasProcessDataEntity.setFormDataId(dataId);
//            paasProcessDataEntity.setBusinessType(businessType);
//            paasProcessDataEntity.setSaasMark(formDataUpdateDTO.getSaasMark());
//            proModelService.updateFormDataStatus(paasProcessDataEntity, FlowStatusEnum.IN_APPROVAL.getType());

            formDataUpdateVO.setFormDataId(dataId);
            return formDataUpdateVO;
        } catch (XbbException e) {
            afterSaveRollBack(formDataUpdateDTO, updateVO, beforeUpdateVO, reRollBackEntity);
            throw e;
        } catch (Exception e) {
            afterSaveRollBack(formDataUpdateDTO, updateVO, beforeUpdateVO, reRollBackEntity);
            LOG.error("customerFormDataUpdateServiceImpl.afterSave 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }


    @Override
    public Long backupData(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, PaasFormEntityExt paasFormEntityExt, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) {
        String corpid = formDataUpdateDTO.getCorpid();
        Integer isProcessForm = paasFormEntityExt.getIsProcessForm();
        if (!formDataUpdateDTO.getIsSatisfyTrigger()) {
            return null;
        }
        JSONObject data = oldPaasFormDataEntity.getData();
        setSystemData(data, oldPaasFormDataEntity);
        long now = DateTimeUtil.getInt();

        data = backupDataOtherInfo(data, corpid, oldPaasFormDataEntity.getId());

        WorkflowBackupDataEntity workflowBackupDataEntity = new WorkflowBackupDataEntity(corpid, oldPaasFormDataEntity.getAppId(), oldPaasFormDataEntity.getFormId(), oldPaasFormDataEntity.getMenuId(), 0L, 0L, oldPaasFormDataEntity.getId(), data, now, now, DelEnum.NORMAL.getDel());
        workflowBackupDataModel.insert(workflowBackupDataEntity);
        //富文本数据备份
        backupRichText(workflowBackupDataEntity, formDataUpdateDTO.getBusinessType(), formDataUpdateDTO.getSaasMark());
        return workflowBackupDataEntity.getId();
    }

    @Override
    public JSONObject backupDataOtherInfo(JSONObject data, String corpid, Long dataId) {
        List<ReturnedPurchaseProductEntity> productList = returnedPurchaseProductModel.getProductsByRetPurchaseId(corpid, dataId);
        data.put(StringConstant.PRODUCT, productList);
        return data;
    }
}
